package com.example.echoewish.ui.my_activity.login_and_register_activity;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.echoewish.R;
import com.example.echoewish.pojo.User;
import com.example.echoewish.econstant.Constant;
import com.example.echoewish.econstant.UserInfo;
import com.example.echoewish.ui.EchoWishCompatActivity;

public class LoginAndRegisterActivity extends EchoWishCompatActivity {

    private Button login_button;
    private Button register_button;
    private EditText login_account_edit_text;
    private EditText login_password_edit_text;

    private LoginAndRegisterViewModel loginAndRegisterViewModel;

    public Handler loginHandler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            Log.d("test_activity", "handleMessage: ");
            switch (msg.what)
            {
                case Constant.LOGIN_FAIL:
                    Toast.makeText(LoginAndRegisterActivity.this,"登录失败，检查密码或者用户名是否准确",Toast.LENGTH_SHORT).show();
                    break;
                case Constant.HTTPS_GET_CALLBACK_SUCCESS_MESSAGE:
                    Toast.makeText(LoginAndRegisterActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                    finish();
                    break;
                case Constant.HTTPS_GET_CALLBACK_FAIL_MESSAGE:
                    Toast.makeText(LoginAndRegisterActivity.this,"登录失败，请检查网络",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_login_and_register);
    }

    @Override
    public void initView() {
        //初始化控件
        login_button=findViewById(R.id.login_button);
        register_button=findViewById(R.id.to_register_button);
        login_account_edit_text=findViewById(R.id.login_account_edit_text);
        login_password_edit_text=findViewById(R.id.login_password_edit_text);

        System.out.println(login_button);
        login_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                User loginUser=new User();
                loginUser.account=login_account_edit_text.getText().toString();
                loginUser.password=login_password_edit_text.getText().toString();
                loginAndRegisterViewModel.newUserMutableLiveData.setValue(loginUser);
            }
        });

        register_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginAndRegisterActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }


    @Override
    public void initViewModel() {
        loginAndRegisterViewModel=new ViewModelProvider(this,new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(LoginAndRegisterViewModel.class);
    }

    @Override
    public void observeLiveData() {
        loginAndRegisterViewModel.newUserMutableLiveData.observe(this, new Observer<User>() {
            @Override
            public void onChanged(User user) {
                loginAndRegisterViewModel.loginOn();
            }
        });

        loginAndRegisterViewModel.userIdMutableLiveData.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                Message message=new Message();
                if(integer.equals(Constant.LOGIN_FAIL))
                {
                    message.what= Constant.LOGIN_FAIL;
                    loginHandler.sendMessage(message);
                }
                else
                {
                    message.what= Constant.HTTPS_GET_CALLBACK_SUCCESS_MESSAGE;
                    UserInfo.getInstance().UserId=integer;
                    UserInfo.getInstance().IsLogin=true;
                    loginHandler.sendMessage(message);
                    loginAndRegisterViewModel.getUserInfo();
                }
            }
        });
    }
}